package com.cxf.controller;

import com.cxf.dao.UserDao;
import com.cxf.pojo.User;
import com.cxf.service.UserService;
import com.cxf.util.MD5Util;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/PersonalCenter")
public class PersonalCenter {

	@RequestMapping("/personalCenter")
	public ModelAndView personalCenter(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		String userName = (String)request.getSession().getAttribute("userName");
		UserService userService = (UserService)ctx.getBean("userServiceImpl");
		User user = userService.getUserByName(userName);
		mv.addObject("user",user);
		mv.addObject("url",request.getSession().getAttribute("url"));
		mv.setViewName("personalCenter");
		return mv;
	}
	
	@RequestMapping("/personalInfo")
	public void personalInfo(HttpServletRequest request,HttpServletResponse response) {
		
	}
	@RequestMapping("/orderInfo")
	public void OrderInfo(HttpServletRequest request,HttpServletResponse response) {
		
	}

   /**
     * 充值金额
     */
	@RequestMapping("/chongzhi")
    public void chongzhi(HttpServletRequest request,HttpServletResponse response) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService)ctx.getBean("userServiceImpl");
        String money = request.getParameter("money");
        response.setContentType("application/json");
        //获取用户信息
        String userName = request.getSession().getAttribute("userName").toString();
        User user = userService.getUserByName(userName);

        //添加金额
        user.setBalance(user.getBalance()+Float.parseFloat(money));
        //保存
        userService.updateUser(user);
        PrintWriter printWriter = null;
        try {
            printWriter = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        printWriter.write("{\"code\":\"1\"}");
        printWriter.close();

    }


}
